package com.example.travelservlet.totalPlan.controller;

import com.example.travelservlet.entity.TotalPlan;
import com.example.travelservlet.totalPlan.service.TotalPlanService;
import com.example.travelservlet.user.domain.Result;
import com.example.travelservlet.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hxy
 * @date 2022/11/9 14:58
 */
@RestController
@RequestMapping("/totalPlan")
public class TotalPlanController {
    @Autowired
    private TotalPlanService totalPlanService;

    /**
     * 在用户登录成功之后就要为用户创建一个总计划表
     * @param userId
     * @return
     */
    @GetMapping("/createTotalPlan/{userId}")
    public Result<TotalPlan> createTotalPlan(@PathVariable String userId){
        TotalPlan totalPlanByUserId = totalPlanService.findTotalPlanByUserId(userId);
        if (totalPlanByUserId==null){
            TotalPlan totalPlan = new TotalPlan();
            totalPlan.setUserId(userId);
            totalPlanService.createTotalPlan(totalPlan);
            return ResultUtil.success(totalPlan);
        }else {
            return ResultUtil.success(totalPlanByUserId);
        }

    }

}
